//
//  ReadCardResultVC.m
//  Gas
//
//  Created by ZhangWeiLiang on 2017/5/31.
//  Copyright © 2017年 HongHu. All rights reserved.
//

#import "ReadCardResultVC.h"
#import "UIViewController+UIViewControllerExt.h"

@interface ReadCardResultVC ()
{
    UIImageView *imgView;
    UILabel *lab;
}

@end

@implementation ReadCardResultVC

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"正在写卡";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, (kScreen_Height-64-150)/2, kScreen_Width, 150)];
    //    baseView.backgroundColor = [UIColor ];
    [self.view addSubview:baseView];
    
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 100)];
    imgView.image = [UIImage imageNamed:@"2-6"];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [baseView addSubview:imgView];
    
    lab = [[UILabel alloc] initWithFrame:CGRectMake(0, imgView.bottom+30, kScreen_Width, 20)];
    lab.text = @"正在写卡...";
    lab.textAlignment = NSTextAlignmentCenter;
    [baseView addSubview:lab];
    
    // 缴费成功后写卡
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kPaySuccessNotification" object:nil];
    
    
    // 写卡结果
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(writeCardAction:) name:@"kWriteCardNotification" object:nil];
}

- (void)writeCardAction:(NSNotification *)notification
{
    NSString *result = notification.object;
    if ([result isEqualToString:@"00"]) {
        
        imgView.image = [UIImage imageNamed:@"2-8"];
        lab.text = @"写卡成功!";
        
        NSString *msg = @"充值成功,一分钟后可使用燃气";
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self popViewController:@"CardRechargeVC"];

        }];
        [alertController addAction:okAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else {
        imgView.image = [UIImage imageNamed:@"2-7"];
        lab.text = @"写卡失败!";
    }
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [self popViewController:@"CardRechargeVC"];
//    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
